package com.vieewer.btdemo.wifimain;

import android.media.MediaPlayer;
import android.media.MediaRecorder;
import android.os.Bundle;
import android.os.Environment;
import android.os.SystemClock;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.*;
import com.vieewer.btdemo.R;

import java.io.File;
import java.io.IOException;

public class RadioActivity extends AppCompatActivity implements View.OnClickListener{

    private LinearLayout mRecord;
    private ImageView mVoice;
    private Button mStatus;
    private Chronometer mTimer;

    private File mRecAudioFile;        // 录制的音频文件
    private File mRecAudioPath;        // 录制的音频文件路徑
    private MediaRecorder mMediaRecorder;// MediaRecorder对象

    private String strTempFile = "recaudio_";// 零时文件的前缀

    private String voicePath;

    private Button mBtnReset;
    private Button mBtnDelete;


    private int flag = 0;




    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_radio);

        initView();

        /* 检测是否存在SD卡 */
        if (Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED))
        {
            mRecAudioPath = Environment.getExternalStorageDirectory();// 得到SD卡得路径
        } else
        {
            Toast.makeText(RadioActivity.this, "没有SD卡", Toast.LENGTH_LONG).show();
        }

    }

    private void initView() {
        mRecord = (LinearLayout)findViewById(R.id.ll_recording);
        mStatus = (Button) findViewById(R.id.tv_click_status);
        mTimer = (Chronometer)findViewById(R.id.chr_timer);

        mBtnReset = (Button)findViewById(R.id.btn_voice_reset);
        mBtnDelete = (Button)findViewById(R.id.btn_voice_delete);
        mBtnReset.setVisibility(View.GONE);
        mBtnDelete.setVisibility(View.GONE);

        mRecord.setOnClickListener(this);
        mBtnDelete.setOnClickListener(this);
        mBtnReset.setOnClickListener(this);
        mStatus.setOnClickListener(this);

        //设置sdcard的路径
//        voicePath = Environment.getExternalStorageDirectory().getAbsolutePath();
//        voicePath += "/audiorecordtest.3gp";

    }

    @Override
    public void onClick(View view) {

        switch (view.getId()) {
            case R.id.tv_click_status :
                if (flag == 0) {
                    mStatus.setText("点击停止录音");
                    mTimer.setVisibility(View.VISIBLE);
                    mTimer.setBase(SystemClock.elapsedRealtime());
                    mTimer.start();

                    try
                    {
//                         ①Initial：实例化MediaRecorder对象
                        mMediaRecorder = new MediaRecorder();
//                         ②setAudioSource/setVedioSource
                        mMediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);//设置麦克风
                        /* ②设置输出文件的格式：THREE_GPP/MPEG-4/RAW_AMR/Default
                         * THREE_GPP(3gp格式，H263视频/ARM音频编码)、MPEG-4、RAW_AMR(只支持音频且音频编码要求为AMR_NB)
                         * */
                        mMediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
//                         ②设置音频文件的编码/**/：AAC/AMR_NB/AMR_MB/Default
                        mMediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
//                         ②设置输出文件的路径
                        try
                        {
                            mRecAudioFile = File.createTempFile(strTempFile, ".mp4", mRecAudioPath);

                            voicePath = mRecAudioPath + "/" + strTempFile + System.currentTimeMillis() + ".mp4";


                        } catch (Exception e)
                        {
                            e.printStackTrace();
                        }
                        mMediaRecorder.setOutputFile(mRecAudioFile.getAbsolutePath());
//                         ③准备
                        mMediaRecorder.prepare();
//                         ④开始
                        mMediaRecorder.start();
//                        按钮状态

                        // TODO Auto-generated method stub
                       /* mMediaRecorder = new MediaRecorder();
                        mMediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
                        mMediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
                        mMediaRecorder.setOutputFile(voicePath);
                        mMediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
                        mMediaRecorder.prepare();

                        mMediaRecorder.start();*/

                } catch (IOException e)
                    {
                        e.printStackTrace();
                    }

                }else if (flag == 1){
                    mStatus.setText("点击试听");
                    mTimer.stop();

                    // TODO Auto-generated method stub
                    if (mRecAudioFile != null)
                    {
                        /* ⑤停止录音 */
                        mMediaRecorder.stop();
                        /* ⑥释放MediaRecorder */
                        mMediaRecorder.release();
                        mMediaRecorder = null;
                    }
                }else if (flag == 2) {
                    //mVoice.setImageResource(R.drawable.icon_record_nor);
                    mStatus.setText("点击开始录音");
                    mBtnReset.setVisibility(View.VISIBLE);
                    mBtnDelete.setVisibility(View.VISIBLE);
                    mTimer.stop();
                    mTimer.setVisibility(View.GONE);

                    mRecord.setEnabled(false);

                    /* 得到被点击的文件 */

                    Log.d("haha", "onClick: " +voicePath);
                    playMusic(mRecAudioFile);


                }

                flag = (flag + 1) % 3;

                break;

            case R.id.btn_voice_reset :
                flag = 0;
                mStatus.setText("点击开始录音");
                mRecord.setEnabled(true);
                mBtnReset.setVisibility(View.GONE);
                mBtnDelete.setVisibility(View.GONE);
                break;

            case R.id.btn_voice_delete :
                if (mRecAudioFile == null) {
                    return;
                }

                mRecAudioFile.delete();
                Toast.makeText(RadioActivity.this,"录音已删除",Toast.LENGTH_SHORT).show();
                break;

            default:

                break;
        }
    }

    /* 播放录音文件 */
    private void playMusic(File  file)
    {
        MediaPlayer player = new MediaPlayer();
        try {
            player.setDataSource(file.getPath());
            player.prepare();
            player.start();
        } catch (IOException e) {
            e.printStackTrace();
        }

       /* // TODO Auto-generated method stub
        MediaPlayer  mPlayer = new MediaPlayer();
        try{
            mPlayer.setDataSource(voicePath);
            mPlayer.prepare();
            mPlayer.start();
        }catch(IOException e){
            Log.e("  ---","播放失败");
        }*/
    }
}

